package com.dnxh.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * The type Image util.
 *
 * @Author 枳湫
 * @Time 2022 /4/13 16:37
 * @Describe Basic_jar
 */
public class ImageUtil {
	
	private static final String CHARSET = "UTF-8";
	private static final int QRCODE_SIZE = 300;
	private static final String FORMAT_NAME = "JPG";
	
	/**
	 * 生成图片
	 *
	 * @param stream  the stream
	 * @param content the content
	 */
	public static void generateImage(OutputStream stream,String content) throws Exception{
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
		hints.put(EncodeHintType.MARGIN, 1);
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
		int width = bitMatrix.getWidth();
		int height = bitMatrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
			}
		}
		ImageIO.write (image,FORMAT_NAME,stream);
	}
	
	/**
	 * method name   : createQrCode
	 * description   : 根据链接生成二维码
	 *
	 * @param url      the url
	 * @param path     the path
	 * @param fileName the file name
	 * @return the string
	 */
	public static String createQrCode (String url, String path, String fileName) {
		try {
			Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String> ();
			hints.put (EncodeHintType.CHARACTER_SET, "UTF-8");
			BitMatrix bitMatrix = new MultiFormatWriter ().encode (url, BarcodeFormat.QR_CODE, 500, 500, hints);
			File file = new File (path, fileName);
			if (file.exists () || ((file.getParentFile ().exists () || file.getParentFile ().mkdirs ()) && file.createNewFile ())) {
				writeToFile (bitMatrix, "jpg", file);
				System.out.println ("文件路径：" + file);
			}
		} catch (Exception e) {
			e.printStackTrace ();
		}
		return null;
	}
	
	/**
	 * method name   : writeToFile
	 * description   : 写文件
	 *
	 * @param matrix the matrix
	 * @param format the format
	 * @param file   the file
	 * @throws IOException the io exception
	 */
	static void writeToFile (BitMatrix matrix, String format, File file) throws IOException {
		BufferedImage image = toBufferedImage (matrix);
		if (! ImageIO.write (image, format, file)) {
			throw new IOException ("Could not write an image of format " + format + " to " + file);
		}
	}
	
	/**
	 * method name   : writeToStream
	 * description   : 写入流
	 *
	 * @param matrix the matrix
	 * @param format the format
	 * @param stream the stream
	 * @throws IOException the io exception
	 */
	static void writeToStream (BitMatrix matrix, String format, OutputStream stream) throws IOException {
		BufferedImage image = toBufferedImage (matrix);
		if (! ImageIO.write (image, format, stream)) {
			throw new IOException ("Could not write an image of format " + format);
		}
	}
	
	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;
	
	/**
	 * method name   : toBufferedImage
	 * description   : 将字节码转换为文件
	 */
	private static BufferedImage toBufferedImage (BitMatrix matrix) {
		int width = matrix.getWidth ();
		int height = matrix.getHeight ();
		BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0 ; x < width ; x++) {
			for (int y = 0 ; y < height ; y++) {
				image.setRGB (x, y, matrix.get (x, y) ? BLACK : WHITE);
			}
		}
		return image;
	}
}
